package com.chaosj.webmagic.scrape;

import com.alibaba.fastjson.JSONObject;
import com.chaosj.webmagic.IScrapeHandler;
import com.chaosj.webmagic.dto.BlogHostEnums;
import com.chaosj.webmagic.dto.GitBookHostEnums;
import com.chaosj.webmagic.md.MdConverterPipeline;
import com.chaosj.webmagic.process.BookStackProcess;
import org.springframework.beans.BeanUtils;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.pipeline.ResultItemsCollectorPipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.lang.management.ManagementFactory;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @className: GitBookHandler
 * @description:
 * @author: caoyangjie
 * @date: 2022/2/18
 **/
public class GitBookHandler implements IScrapeHandler<String, String> {
    private ConcurrentHashMap<String, ResultItemsCollectorPipeline> collectorPipelineMap = new ConcurrentHashMap<>();
    private ConcurrentHashMap<GitBookHostEnums, PageProcessor> processors = new ConcurrentHashMap<>();
    private ConcurrentHashMap<BlogHostEnums, Downloader> downloaders = new ConcurrentHashMap<>();
    private JSONObject params;

    @Override
    public void scrape(String request) {
        GitBookHostEnums gitBook = GitBookHostEnums.position(request);
        PageProcessor processor = processors.computeIfAbsent(gitBook, blogType-> BeanUtils.instantiateClass(blogType.getProccess()));
        Spider spider = Spider.create(processor).addUrl(request);
        spider.thread(ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors()).run();
        ((BookStackProcess)processor).hasErrorUrl();
    }

    public GitBookHandler setParams(JSONObject json){
        this.params = json;
        return this;
    }

    @Override
    public String getResult(String requestDto) {
        return requestDto;
    }
}
